<?php
namespace yan\googlebusiness\yii2;
use Yii;
use frontend\modules\product\v2\models\ProductBin as FrontendProduct;
class Action extends \backend\components\actions\CAction{
    private $_google_business;
    public $view = 'index';
    protected $_viewPath = __DIR__ . '/views';
    public function run(){
        $message = null;
        if(Yii::$app->request->get('flag') == 'async'){
            $message = $this->asyncDo();
        }else if(Yii::$app->request->get('flag') == 'delete'){
            $message = $this->deleteAll();
        }
        return $this->render(['message'=>$message]);
    }
    public function getGoogleBusiness(){
        if(empty($this->_google_business)){
            $facotry = \yan\googlebusiness\GoogleBusiness::instance();
            $facotry->init([
                'merchantId'=>Yii::$app->params['google.merchantId'],
                'client_id'=>Yii::$app->params['google.client_id'],
                'client_secret'=>Yii::$app->params['google.client_secret'],
                'credentials_file' => Yii::$app->params['google.credentials_file'],
            ]);
            $facotry->authenticate();
            $this->_google_business = $facotry;
        }
        return $this->_google_business;
    }

    public function asyncDo(){
        $query = FrontendProduct::find()->where(['status'=>1])->andWhere(['>','quanity',0])->andWhere(['>','price',0]);
        $categorys = [];
        $products = [];
        Yii::$app->session['lang_id'] = '3';
        foreach($query->each() as $product){
            if(!empty($product->wine->variety) && !empty($categorys[$product->wine->variety])){
                $categoryMeta = $categorys[$product->wine->variety];
            }else if(!empty($product->wine->variety)){
                $categoryMeta = \backend\modules\category\models\Category::find()
                    ->with(['meta'])
                    ->where(['id'=>$product->wine->variety])
                    ->asArray()->one();
                $categorys[$product->wine->variety] = $categoryMeta;
            }
            $googleProdut = new \yan\googlebusiness\model\Product();
            $googleProdut->id = $product->sku;
            $googleProdut->offerId = $product->id;
            $googleProdut->title = $product->meta->name;
            $googleProdut->description = !empty($product->meta->description) ? $product->meta->description : $product->meta->name;
            $googleProdut->description = $googleProdut->description . ' 18歲以下未成年人不得購買酒精類產品';
            $googleProdut->link = \common\utils\FunUtil::showFrontentUrlByUrl(['/product/default/detail','id'=>$product->sku]);
            $googleProdut->image_link = \common\utils\FunUtil::showImg(!empty($product->image) ? $product->image : 'uploads/nopic.jpg');
            $googleProdut->setQuanitity($product->quanity);
            $googleProdut->setPrice($product->price);
            $googleProdut->brand = !empty($categoryMeta['meta']) ? $categoryMeta['meta']['name'] : 'Red Wine';
            $googleProdut->color = !empty($categoryMeta['meta']) ? $categoryMeta['meta']['name'] : 'Red Wine';
            $googleProdut->gtin = '';
            $googleProdut->mpn = $product->sku;
            $products[] = $googleProdut->toArray();
        }
        if(!empty($products)){
            
            $service = $this->getGoogleBusiness();
            return $service->product->insertProductBatch($products);
        }
        return null;
    }
    public function deleteAll(){
        $query = FrontendProduct::find();
            
        $ids = [];
        foreach($query->each() as $product){
            $ids[] = $product->id;
        }
        if(!empty($ids)){
            $service = $this->getGoogleBusiness();
            return $service->product->deleteProductBatch($ids);
        }
        return [];
    }
}